package com.os.serviceimpl;

import java.util.List;

import com.os.dao.DeskDaoH;
import com.os.dao.DeskDaoS;
import com.os.dao.StoreDaoH;
import com.os.daoimpl.DeskDaoImplH;
import com.os.daoimpl.DeskDaoImplS;
import com.os.daoimpl.StoreDaoImplH;
import com.os.model.DeskH;
import com.os.service.DeskServiceS;

import net.sf.json.JSONObject;

public class DeskServiceImplS implements DeskServiceS {
	
	DeskDaoS dd = new DeskDaoImplS();
	DeskDaoH ddao = new DeskDaoImplH();
	StoreDaoH sdao = new StoreDaoImplH();
	
	@Override
	public int findDeskCountS() {
		// TODO Auto-generated method stub
		return ddao.getAllDesknumH();
	}

	@Override
	public List<DeskH> findDeskS(int page, int rows) {
		// TODO Auto-generated method stub
		return ddao.getAllDeskH(page, rows);
	}

	@Override
	public JSONObject loadDeskH(int page, int rows) {
		JSONObject jo = new JSONObject();
		List<DeskH> desks = ddao.getAllNoAuditDeskH(page, rows);
		int total = ddao.getAllNoAuditDesknumH();
		jo.put("total", total);
		jo.put("rows", desks);
		return jo;
	}

	@Override
	public JSONObject checkDeskH(int oid) {
		DeskH desk = ddao.getDeskByOidH(oid);
		StringBuffer fnames = ddao.getFnamesByFoodlistH(desk.getFoodlist());
		desk.setFoodname(fnames.toString());
		double fprice = ddao.getFpriceByFoodlist(desk.getFoodlist());
		desk.setFprice(fprice);
		String wname = ddao.getWnameByWidH(desk.getWid());
		desk.setWname(wname);
		JSONObject jo = JSONObject.fromObject(desk);
		return jo;
	}

	@Override
	public int updateDeskFstateH(int fstate, int oid) {
		DeskH oldd = ddao.getDeskByOidH(oid);
		int oldFstate = oldd.getFstate();
		int flag = ddao.setDeskByFstateH(fstate, oid);
		if (flag==0) {
			return flag;
		}else{		
			DeskH newd = ddao.getDeskByOidH(oid);
			if (newd.getFstate() == 1) {
				String foodlist = newd.getFoodlist();
				String [] fids = foodlist.split(",");
				for (int i = 0; i < fids.length; i++) {
					int fid = Integer.parseInt(fids[i]);
					int fnums = sdao.getFnumsByFid(fid);
					if (fnums==0) {
						ddao.setDeskByFstateH(oldFstate, oid);
						if (i==0) {
							flag = 0;
							break;							
						}else{
							for (int j = 0; j < i; j++) {
								int fid2 = Integer.parseInt(fids[j]);
								int fnums2 = sdao.getFnumsByFid(fid2);
								sdao.updateStoreH(fid2, fnums2+1);
							}
							flag = 0;
							break;
						}
					}
					flag = sdao.updateStoreH(fid, fnums-1);
					if (flag==0) {
						ddao.setDeskByFstateH(oldFstate, oid);
						if (i==0) {
							flag = 0;
							break;							
						}else{
							for (int j = 0; j < i; j++) {
								int fid2 = Integer.parseInt(fids[j]);
								int fnums2 = sdao.getFnumsByFid(fid2);
								sdao.updateStoreH(fid2, fnums2+1);
							}
							flag = 0;
							break;
						}
					}
				}
			}
			return flag;
		}
	}

	@Override
	public boolean addDesksS(int did, String foodlist, int wid,int fnum,String ftime) {
		// TODO Auto-generated method stub
		return dd.addDeskS(did, foodlist, wid,fnum,ftime);
	}

	@Override
	public int findFnum(int oid) {
		// TODO Auto-generated method stub
		return dd.selectFnum(oid);
	}
}
